import { BaseViewProp } from "./BaseViewProp";
import UI_view_checkbox from "../../../ui/Main/UI_view_checkbox";

export class PropToggle extends BaseViewProp<UI_view_checkbox> {

    private _isChecked: boolean;
    public get isChecked(): boolean {
        return this._isChecked;
    }

    public set isChecked(value: boolean) {
        if (this._isChecked == value) return;
        this._isChecked = value;
        this.ui.m_state_check.selectedIndex = this._isChecked ? 1 : 0;
    }

    public onToggleChanged: Laya.Handler;

    _OnInited() {
        this._isChecked = false;
        this.ui.m_state_check.selectedIndex = 0;
        this.ui.onClick(this, this._OnClickToggle);
    }

    _OnDispose() {
        if (this.onToggleChanged != null) {
            this.onToggleChanged.recover();
            this.onToggleChanged = null;
        }
    }

    private _OnClickToggle() {
        this.isChecked = !this.isChecked;
        this.onToggleChanged?.runWith(this.isChecked);
    }

}